package com.obtk.controllerSGW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.modelSGW.ServerType;
import com.obtk.service.ServerTypeService;
import com.obtk.util.*;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/serType")
@Controller
public class ServerTypeController {

    @Autowired
    private ServerTypeService serverTypeService;

    /**
     * 服务列表数据
     */
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo getSerType(ServerType serverType, Integer limit, Integer offset) {
        PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
        List<ServerType> serverTypeList = serverTypeService.getSerType(serverType);
        System.out.println("ceshi" + serverTypeList);
        PageInfo<ServerType> pages = new PageInfo<>(serverTypeList);
        return ResultUtil.table(serverTypeList, pages.getTotal());
    }

    /**
     * 服务查询
     */
    @PostMapping("/BySerId")
    @ResponseBody
    public ServerType serTypeById(ServerType serverType) {
        System.out.println(serverType);
        return serverTypeService.selectByType(serverType);
    }

    /**
     * 新增服务类型
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(ServerType typeForm) {
        String serType = typeForm.getSerType();
        System.out.println(serType);
        if (serType != null) {
            ServerType serverType = serverTypeService.getServerTypeByType(serType);
            System.out.println(serverType);
            if (null != serverType) {
                return ResultUtil.error("服务类型已存在");
            } else {
                int num = serverTypeService.registerType(typeForm);
                if (num > 0) {
                    return ResultUtil.success("添加服务成功");
                } else {
                    return ResultUtil.error("添加服务失败");
                }
            }
        } else {
            return ResultUtil.error("不能为空");
        }
    }

    /**
     * 删除服务
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String serId) {
        String[] serIdsList = serId.split(",");
        int a = serverTypeService.updateStatusBatch(serIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("解除合作成功");
        } else {
            return ResultUtil.error("解除失败");
        }
    }

    /**
     * 服务查询
     */
    @GetMapping("/showType")
    @ResponseBody
    public ResponseVo selectType(Integer status) {
        System.out.println(status);
        List<ServerType> array = serverTypeService.getServerType(status);
        return ResultUtil.success("查询成功",array);
    }
}